package com.huike.service.impl;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.dto.DataScopeDto;
import com.huike.dto.SysRoleDto;
import com.huike.dto.SysRolePageDto;
import com.huike.entity.SysMenu;
import com.huike.entity.SysRole;
import com.huike.entity.SysRoleMenu;
import com.huike.exception.BaseException;
import com.huike.mapper.MenuMapper;
import com.huike.mapper.RoleMapper;
import com.huike.mapper.SysRoleMenuMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 角色列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result listRole(SysRolePageDto dto) {
        // 1.数据校验
        if (dto == null) {
            throw new BaseException("数据异常");
        }
        // 2.条件查询
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        if (dto.getRoleName() != null && !dto.getRoleName().equals("")) {
            lqw.like(SysRole::getRoleName,dto.getRoleName());
        }
        if (dto.getStatus() != null) {
            lqw.eq(SysRole::getStatus,dto.getStatus());
        }
        lqw.orderByAsc(SysRole::getRoleSort);

        // 3.分页查询
        IPage page = new Page(dto.getPageNum(),dto.getPageSize());

        page = roleMapper.selectPage(page, lqw);

        PageResult pageResult = new PageResult(page.getTotal(),page.getRecords());
        return Result.success(pageResult);
    }

    /**
     * 获取所有菜单名称
     *
     * @return
     */
    @Override
    public Result allMenuName() {
        List<SysMenu> menuList = menuMapper.allMenuName();
        return Result.success(menuList);
    }

    /**
     * 创建角色
     *
     * @param dto
     * @return
     */
    @Override
    public Result saveRole(SysRoleDto dto) {
        // 1.校验数据
        if (dto == null) {
            throw new BaseException("数据错误");
        }
        // 2.属性拷贝
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(dto,sysRole);
        sysRole.setCreateTime(LocalDateTime.now());
        // TODO 获取创建用户
        sysRole.setCreateBy("admin");
        roleMapper.insert(sysRole);

        Long roleId = sysRole.getRoleId(); // 角色id

//        List<Long> menuIds = dto.getMenuIds();
//        for (Long menuId : menuIds) {
//            SysRoleMenu sysRoleMenu = new SysRoleMenu();
//            sysRoleMenu.setRoleId(roleId);
//            sysRoleMenu.setMenuId(menuId);
//            sysRoleMenuMapper.insert(sysRoleMenu);
//        }

        return Result.success();
    }

    /**
     * 回显
     *
     * @param roleId
     * @return
     */
    @Override
    public Result findById(Long roleId) {
        SysRole sysRole = roleMapper.selectById(roleId);
        return Result.success(sysRole);
    }

    /**
     * 修改权限
     *
     * @return
     */
    @Override
    public Result updateDataScope(DataScopeDto dto) {

        SysRole sysRole = roleMapper.selectById(dto.getRoleId());
        sysRole.setDataScope(dto.getDataScope());
        sysRole.setRoleKey(dto.getRoleKey());
        roleMapper.updateById(sysRole);

        return Result.success();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Result update(SysRoleDto dto) {

        // 先删除菜单角色关联表
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        sysRoleMenuMapper.delete(lqw.eq(SysRoleMenu::getRoleId,dto.getRoleId()));

        SysRole sysRole = roleMapper.selectById(dto.getRoleId());
        sysRole.setRoleName(dto.getRoleName());
        sysRole.setRoleKey(dto.getRoleKey());
        sysRole.setRoleSort(dto.getRoleSort());
        sysRole.setStatus(dto.getStatus());
        // 保存
        roleMapper.updateById(sysRole);

        // 保存菜单
        List<Long> menuIds = dto.getMenuIds();
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenuMapper.insert(sysRoleMenu);
        }

        return Result.success();
    }

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @Override
    public Result deleteById(Long roleId) {
        roleMapper.deleteById(roleId);
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        sysRoleMenuMapper.delete(lqw.eq(SysRoleMenu::getRoleId,roleId));
        return Result.success();
    }
}
